<?php

class Mod_user extends Model {

    function Mod_user() {
        parent::model();
    }

    function index() {
        
    }

    //=======================this is function for add new user====================//
    
    function add() {
       
      //  echo $getname; die();
        $username = $this->input->post('txtuser');
        $password = $this->input->post('txtpassword');
        $confpass = $this->input->post('txtcon_word');
        $sex = $this->input->post('txtsex');
        $position = $this->input->post('txtposition');
        $email = $this->input->post('txtemail');
         $getname=$this->db->query('SELECT use_username FROM tbl_user');
         foreach($getname->result() as $rows){// looping for all recording in tbl_user
             $getuser=$rows->use_username;
             $emails=$rows->use_email;
             if($username==$getuser){//do not allow the same name of user
             $this->session->set_userdata('invalid_user', '<p style="color:red;">Can not insert user with the same name!</p>');
             redirect('user/add');
            }else if($email==$emails){//do not allow the same  email of user
               $this->session->set_userdata('invalid_email', '<p style="color:red;">Can not insert email with the same name!</p>');
             redirect('user/add');
            }
         }
    
        
       if ($password !== $confpass) {//comparation of passward with confirmation of password
            $this->session->set_userdata('invalid_pas', '<p style="color:red;">password and confirm password is not match</p>');
            redirect('user/add');
        }

        $data = array(
            'use_username' => $username,
            'use_sex' => $sex,
            'use_email' => $email,
            'use_password' => md5($password),
            'use_confirmpassword ' => md5($confpass),
            'user_type' => $position,
            'use_status'=>1,
        );

        return $this->db->insert('tbl_user', $data);
    }

    //===============================function for using select data fram user==============//
    
    function selectuser() {
        //$username=$this->input->post('txtname');
        $this->db->select('*');
       // $this->db->where('use_username',$username);
        return $this->db->get('tbl_user');
    }

    //===================select data to edit====================//
    
    function selet_edit() {
        $id = $this->uri->segment('3');
        $this->db->where('use_id', $id);
        return $this->db->get('tbl_user');
    }

    //=======================function used for checkedit data======================//

    function checkedit() {
        $username = $this->input->post('txtuser');
        $sex = $this->input->post('txtsex');
        $position = $this->input->post('txtposition');
        $email = $this->input->post('txtemail');

        $data = array(
            'use_username' => $username,
            'use_sex' => $sex,
            'use_email' => $email,
            'user_type' => $position
        );
        $id = $this->uri->segment('3');
        $this->db->where('use_id', $id);
        return $this->db->update('tbl_user', $data);
    }

    //=========================function for delete data from tbl_user========================//
    

    function delete() {
        $id = $this->uri->segment('3');
        return $this->db->delete('tbl_user', array('use_id' => $id));
    }
    
    //--test----
    
      function get_user($p=null) { // $p is array
        if ($this->input->post($this->dbf->use_username) && $this->input->post($this->dbf->use_username) != '')
        $this->db->like($this->dbf->use_username, $this->input->post($this->dbf->use_username));
        $this->db->limit($p[0], $p[1]);
        $this->db->where('use_status',1);
        $this->db->order_by('use_id','desc');
        return $this->db->get('tbl_user');
    }

}